package com.chilly.other.datastructer;

import lombok.var;

/**
 * 基于单向链表实现栈
 * <p>
 * Created by Chilly Cui on 2021/1/7.
 */
public class LLStack<T> {
    private LLNode<T> headNode;

    public LLStack() {
        this.headNode = new LLNode<>();
    }

    public boolean isEmpty() {
        return headNode == null;
    }

    //栈顶
    public T top() {
        if (headNode == null) {
            return null;
        } else {
            return headNode.getData();
        }
    }

    //入栈
    public void push(T data) {
        if (isEmpty()) {
            headNode = new LLNode<>(data);
        } else if (headNode.getData() == null) {
            headNode.setData(data);
        } else {
            var llNode = new LLNode<>(data);
            llNode.setNext(headNode);
            headNode = llNode;
        }
    }

    //出栈
    public T pop() {
        if (isEmpty()) {
            return null;
        } else {
            var data = headNode.getData();
            headNode = headNode.getNext();
            return data;
        }
    }

}
